package recharge

import (
	"context"
	"errors"

	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"
	"github.com/zeromicro/go-zero/core/logc"
	"gorm.io/gorm"
)

type RechargeChannelModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewRechargeChannelModel(ctx context.Context, db *gorm.DB) *RechargeChannelModel {
	return &RechargeChannelModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *RechargeChannelModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

// GetRechargeChannelById 根据渠道id获取渠道
func (model *RechargeChannelModel) GetRechargeChannelById(Id int64) (schema.RechargeChannel, error) {
	var channel schema.RechargeChannel
	res := model.getDb().Model(&schema.RechargeChannel{}).Where("id =?", Id).First(&channel)
	if err := res.Error; errors.Is(err, gorm.ErrRecordNotFound) {
		logc.Error(model.ctx, "RechargeChannelModel-GetChannelById-0, err", err)
		return schema.RechargeChannel{}, xerrors.ErrNoRows
	} else if err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-GetChannelById-1, err", err)
		return schema.RechargeChannel{}, err
	}

	return channel, nil
}

// GetRechargeChannelListByTypeId 根据充值类型获取充值渠道
func (model *RechargeChannelModel) GetRechargeChannelListByTypeId(typeId int64, page, pageSize uint32) ([]schema.RechargeChannel, int64, error) {
	var (
		channels []schema.RechargeChannel
		total    int64
	)

	db := model.getDb().Model(&schema.RechargeChannel{})

	db = db.Where("type = ?", typeId)

	// Count total records
	if err := db.Count(&total).Error; err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-GetRechargeChannelListByTypeId-count,err", err)
		return nil, 0, err
	}

	// Fetch paginated records
	offset := (page - 1) * pageSize
	res := db.Offset(int(offset)).Limit(int(pageSize)).Order("sort asc").Find(&channels)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-GetRechargeChannelListByTypeId, err", err)
		return nil, 0, err
	}

	return channels, total, nil
}

// GetRechargeChannelByCode 根据三方映射码获取充值渠道
func (model *RechargeChannelModel) GetRechargeChannelByCode(code string) (schema.RechargeChannel, error) {
	var channel schema.RechargeChannel
	res := model.getDb().Model(&schema.RechargeChannel{}).Where("mapping_code =?", code).First(&channel)
	if err := res.Error; errors.Is(err, gorm.ErrRecordNotFound) {
		logc.Error(model.ctx, "RechargeChannelModel-GetRechargeChannelByCode-0, err", err)
		return schema.RechargeChannel{}, xerrors.ErrNoRows
	} else if err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-GetRechargeChannelByCode-1, err", err)
		return schema.RechargeChannel{}, err
	}

	return channel, nil
}

// GetAllRechargeChannels 获取所有充值通道
func (model *RechargeChannelModel) GetAllRechargeChannels(filter map[string]interface{}, page, pageSize uint32) ([]schema.RechargeChannel, int64, error) {
	var (
		channels []schema.RechargeChannel
		total    int64
	)

	db := model.getDb().Model(&schema.RechargeChannel{})

	// 进行筛选
	if v, ok := filter["type"]; ok {
		db = db.Where("type = ?", v)
	}
	if v, ok := filter["name"]; ok {
		db = db.Where("name LIKE ?", "%"+v.(string)+"%")
	}
	if v, ok := filter["status"]; ok {
		db = db.Where("status = ?", v)
	}

	// Count total records
	if err := db.Count(&total).Error; err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-GetAllRechargeChannels-count,err", err)
		return nil, 0, err
	}

	// Fetch paginated records
	offset := (page - 1) * pageSize
	res := db.Offset(int(offset)).Limit(int(pageSize)).Order("sort asc").Find(&channels)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-GetAllRechargeChannels, err", err)
		return nil, 0, err
	}

	return channels, total, nil
}

// UpdateRechargeChannel 更新充值通道
func (model *RechargeChannelModel) UpdateRechargeChannel(id int64, updateData map[string]interface{}) error {
	res := model.getDb().Model(&schema.RechargeChannel{}).Where("id = ?", id).Updates(updateData)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-UpdateRechargeChannel, err", err)
		return err
	}
	return nil
}

// DeleteRechargeChannel 删除充值通道
func (model *RechargeChannelModel) DeleteRechargeChannel(id int64) error {
	res := model.getDb().Model(&schema.RechargeChannel{}).Where("id = ?", id).Delete(&schema.RechargeChannel{})
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-DeleteRechargeChannel, err", err)
		return err
	}
	return nil
}

// DeleteRechargeChannels 批量删除充值通道
func (model *RechargeChannelModel) DeleteRechargeChannels(ids []int64) error {
	if len(ids) == 0 {
		return nil
	}
	err := model.getDb().Where("id IN (?)", ids).Delete(&schema.RechargeChannel{}).Error
	if err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-DeleteRechargeChannels, err", err)
	}
	return err
}

// RechargeChannelCreate 创建充值通道
func (model *RechargeChannelModel) RechargeChannelCreate(channel *schema.RechargeChannel) error {
	res := model.getDb().Create(channel)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeChannelModel-RechargeChannelCreate, err", err)
		return err
	}
	return nil
}
